package cn.javase.io_model.nio._01_Buffer;

import java.nio.ByteBuffer;
import org.junit.jupiter.api.Test;

public class _缓冲区常用API {

    @Test
    public void test01() {
        // 1. 新建一个缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(10);
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        System.out.println("-------------------");
        // 2. put往缓冲区中添加数据
        String str = "it";
        buffer.put(str.getBytes());
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        System.out.println("-------------------");
        // 3. flip方法切换到可读的模式
        buffer.flip();
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        System.out.println("-------------------");
        // 4. get读取数据
        byte bytes = buffer.get();
        System.out.println((char) bytes);
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        System.out.println("-------------------");
        // 5. 清除数据
        buffer.clear();
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        System.out.println((char) buffer.get());
    }

    @Test
    public void test02() {
        // 1. 新建一个缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(10);
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        System.out.println("-------------------");
    }

}
